package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.account.client.UserAccountFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Qualifier("com.atguigu.tingshu.order.client.OrderInfoFeignClient")
    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;


    @Qualifier("com.atguigu.tingshu.account.client.UserAccountFeignClient")
    @Autowired
    private UserAccountFeignClient userAccountFeignClient;

    @Override
    public PaymentInfo savePaymentInfo(String wxPayItemType, String orderNo) {
        Long userId = AuthContextHolder.getUserId();

        //1.根据订单编号查询该笔订单是否支付过
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderNo, orderNo);
        wrapper.eq(PaymentInfo::getUserId, userId);
        wrapper.eq(PaymentInfo::getPaymentStatus, "1402");
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(wrapper);

        if (paymentInfo == null) {
            //记录
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(userId);
            paymentInfo.setPaymentType(wxPayItemType);  //支付类型
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setOutTradeNo(orderNo); //微信回调我的时候给他赋值 out_trade_no
            //下单商品
            if ("1301".equals(wxPayItemType)){
                Result<OrderInfo> orderInfoByOrderNo = orderInfoFeignClient.getOrderInfoByOrderNo(orderNo, userId);
                OrderInfo orderInfoByOrderNoData = orderInfoByOrderNo.getData();
                Assert.notNull(orderInfoByOrderNoData,"远程查询订单微服务获取订单信息失败");
                String payWay = orderInfoByOrderNoData.getPayWay();
                paymentInfo.setPayWay(payWay);  //支付方式 wx ali recharge
                paymentInfo.setAmount(orderInfoByOrderNoData.getOrderAmount()); //
                paymentInfo.setContent(orderInfoByOrderNoData.getOrderDetailList().get(0).getItemName());  //商品内容
            }else {
                //充值钱
                Result<RechargeInfo> rechargeInfoResult = userAccountFeignClient.getRechargeInfoByOrderNo(orderNo,userId);
                RechargeInfo rechargeInfo = rechargeInfoResult.getData();
                Assert.notNull(rechargeInfo,"远程查询账户微服务获取充值订单信息失败");
                paymentInfo.setPayWay(rechargeInfo.getPayWay());  //支付方式 wx ali recharge
                paymentInfo.setAmount(rechargeInfo.getRechargeAmount()); //
                paymentInfo.setContent("充钱了");  //商品内容
            }
            paymentInfo.setPaymentStatus("1401");
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setCallbackContent("");
            paymentInfoMapper.insert(paymentInfo);
        }
        //返回支付对象
        return paymentInfo;
    }
}
